package com.homework.controller;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.homework.service.*;
import com.homework.shiro.AccountProfile;
import com.homework.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
public class BaseController {

    @Autowired
    HttpServletRequest req;

    @Autowired
    PostService postService;

    @Autowired
    UserService userService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    UserCollectionService userCollectionService;

    @Autowired
    UserMessageService userMessageService;

    @Autowired
    CommentService commentService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    ObjectMapper objectMapper;

    protected AccountProfile getProfile() {
        Object principal = SecurityUtils.getSubject().getPrincipal();
        try {
            return objectMapper.readValue(JSONUtil.toJsonStr(principal), AccountProfile.class);
        } catch (IOException e) {
           log.error("获取AccountProfile失败！", e);
           return new AccountProfile();
        }
    }

    protected Long getProfileId() {
        return getProfile().getId();
    }

}
